import { loginAPI, getRoleInfoAPI } from '@/api'
import { getToken, removeToken, setToken } from '@/utils/auth'

export default {
  state: {
    token: getToken(),
    roleInfo: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    setRoleInfo(state, preload) {
      state.roleInfo = preload
    }
  },
  actions: {
    async login(store, data) {
      store.commit('setToken', (await loginAPI(data)).token)
    },
    async getRoleInfo(store) {
      store.commit('setRoleInfo', await getRoleInfoAPI())
    },
    logout(store) {
      store.commit('removeToken')
      store.commit('setRoleInfo', {})
    }
  },
  namespaced: true
}
